from flask import Blueprint, request, json, jsonify
from comm import get_data

bp_course = Blueprint("course", __name__)

from db import db1


@bp_course.route('/couponM', methods=['POST', "GET", "PUT", 'DELETE'])
def couponM():
    # 添加
    if request.method == "POST":
        # 获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        # 构造sql语句
        sql = "insert into coupon values(0,'%s',%d,'%s','%s')" % (
            data['name'], int(data['total']), data['stime'], data['etime'])
        # pymysql
        db1.insert(sql)
        # 返回结果
        return 'ok'
    elif request.method == 'GET':
        id = request.args.get('id')
        sql = "select * from coupon where id=%d" % (int(id))
        res = db1.find(sql)
        return jsonify({"code": 200, 'coupon': res})

    elif request.method == 'PUT':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "update coupon set name='%s',total=%d,starttime='%s',endtime = '%s' where id = % d" % (
            data['name'], int(data['total']), data['stime'], data['etime'], int(data['id']))
        db1.insert(sql)
        return jsonify({"code": 200})

    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = "delete from coupon where id=%d" % (int(id))
        db1.insert(sql)
        return jsonify({"code": 200})


@bp_course.route('/deptM', methods=['POST', 'GET'])
def deptM():
    if request.method == "POST":
        data = get_data()
        # 判断名字是否已经存在
        sql = "select id from dept where name='%s'" % (data['name'])
        res = db1.find(sql)
        # 存在直接返回
        if res:
            return jsonify({"code": 10011, 'mes': '已经存在'})
        # 不存在添加
        else:
            sql = "insert into dept(name) values('%s')" % (data['name'])
            db1.insert(sql)
            return jsonify({"code": 200})
    else:
        sql = "select * from dept"
        res = db1.findall(sql)
        return jsonify({"code": 200, 'list': res})


from .myfile import myfile
import os


@bp_course.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    myfile.file_mkdir(2)
    file.save(os.path.join('./upload/2/', file.filename))
    return 'ok'


@bp_course.route('/scenicm', methods=['POST', 'GET'])
def scenicm():
    if request.method == "POST":
        data = request.form
        sql = "insert into scenic(title,price,typeid) values('%s',%.2f,%d)" % (
            data['title'], float(data['price']), int(data['typeid']))
        db1.insert(sql)
        sql1 = "select id from scenic where title='%s'" % (data['title'])
        res = db1.find(sql1)
        file = request.files['file']

        myfile.file_mkdir(str(res['id']))
        file.save(os.path.join('./static/upload/' + str(res['id']), file.filename))
        sql3 = "update scenic set pic='%s' where id=%d" % (
            '/static/upload/' + str(res['id']) + "/" + file.filename, int(res['id']))
        db1.insert(sql3)
        return jsonify({"code": 200})
    else:
        sql = 'select * from scenic'
        res = db1.findall(sql)
        return jsonify({"code": 200, 'list': res})


@bp_course.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    cityid = request.form['cityid']
    # 创建文件夹
    myfile.file_mkdir(cityid)
    # 图片上传
    file.save(os.path.join('./static/upload/' + str(cityid), file.filename))
    return jsonify({"code": 200, 'url': '/static/upload/' + str(cityid) + '/' + file.filename})


@bp_course.route('/citym')
def citym():
    sql = "select id,name from city"
    res = db1.findall(sql)
    return jsonify({"code": 200, 'list': res})


@bp_course.route('/prom', methods=['POST'])
def prom():
    data = get_data()
    sql = "insert into product(title,price,city_id,pic) values('%s',%.2f,%d,'%s')" % (
        data['title'], float(data['price']), int(data['city_id']), data['pic'])
    db1.insert(sql)
    return jsonify({"code": 200})
